package com.germinus.cardea.bbdd;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.hql.ast.SqlASTFactory;

import com.germinus.cardea.util.HibernateUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;


public class MedicineManager {

	static private MedicineManager medicineManager;

	@SuppressWarnings("unchecked")
//	public static void main(String[] args) {
//        MedicineManager mgr = new MedicineManager();
//        if (args[0].equals("store")) {
//        	
//        	List<MedicineType> types =mgr.listMedicineType("Paracetamol");
//        	
//        	System.out.println("Lista: "+ types.size());
//        	if(types != null){
//        		MedicineType mtype=types.get(0);
//            	Medicine med=mgr.createAndStoreMedicine("0051PAR", new Date(), "Farmacia", mtype);
//            	MedicineTypeEvent eventType = new MedicineTypeEvent()
//            	mgr.createandStoreMedEvent(med, "add");
//        	}else {
//        		MedicineType mtype= mgr.createAndStoreMedicineType("Paracetamol", "ROBY"); 
//        		Medicine med=mgr.createAndStoreMedicine("0051PAR", new Date(), "Farmacia", mtype);
//        		mgr.createandStoreMedEvent(med, "add");
//        	}
//        	
//        }else if (args[0].equals("list")) {
//            
//        	List<Medicine> meds = mgr.listMedicines();
//            for (int i = 0; i < meds.size(); i++) {
//            	Medicine theMedicine = meds.get(i);
//                System.out.println("Code: " + theMedicine.getTagId() +
//                                   " Caducity Date: " + theMedicine.getExpDate()+
//                                   " Localization: "+ theMedicine.getLocalization());
//            }
//            
//        }else if (args[0].equals("add")){
//        	mgr.addMedicineType("Aspirina", "ROBY");
//        	mgr.addMedicineType("Hibuprofeno", "ROBY");
//        	mgr.addMedicineType("Paracetamol", "ROBY");
//        	
//        }else if (args[0].equals("drop")){
//        	
//        	List<Medicine> meds = mgr.getMedicine("0048HIB");
//        	List<>
//        	for(int i=0; i< meds.size(); i++){
//        		Medicine theMedicine = meds.get(i);
//        		mgr.dropMedicine(theMedicine);
//        		mgr.createandStoreMedEvent(theMedicine, "remove");
//        	}
//        	
//        	
//        }else if(args[0].equals("stock")){
//        	
//        	List<MedicineType> l=mgr.listMedicineType("Hibuprofeno");
//        	int stock=mgr.getstock(l.get(0));
//        	mgr.createAndStoreStockEvent(stock, l.get(0), "stock");
//        }
//
//        HibernateUtil.getSessionFactory().close();
//    }
//	
	static public MedicineManager getMedicineManager(){
		
		if(medicineManager==null){
			medicineManager = new MedicineManager();
		}		
		return medicineManager;
	}
	
	public void closeHibernate(){
		
		HibernateUtil.getSessionFactory().close();
	}
	
	private void createAndStoreStockEvent(int Stock, MedicineType medtype, StockTypeEvent evType, String message){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();
        
        StockEvent ev= new StockEvent();
        ev.setDate(new Date());
        ev.setMedicineType(medtype);
        ev.setStock(Stock);
        ev.setTypeEventId(evType);
        ev.setMessage(message);
        session.save(ev);

        session.getTransaction().commit();
	}
	
	public int getstock(MedicineType medtype){
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Query q=session.createQuery("select med from Medicine as med where med.type = :type");
        q.setParameter("type", medtype);
        
        List result = q.list();
        	
        session.getTransaction().commit();

        return result.size();
	}
	
	
	public void dropMedicine (Medicine medicine){
		
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();
    	
        //medicine.setLocalization("Paciente Externo");
		session.update(medicine);
    	
        session.getTransaction().commit();
	}
	
	public List<Medicine> getMedicine (String medicineTag){
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Query q=session.createQuery("select med from Medicine as med where med.tagId = :code");
        q.setParameter("code", medicineTag);
        
        List result = q.list();
        	
        session.getTransaction().commit();

        return result;
	}
	
	public MedicineType createAndStoreMedicineType(String name, String manufacturer){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();
        
        MedicineType medType = new MedicineType();
        medType.setManufacturer(manufacturer);
        medType.setName(name);
        
        session.save(medType);

        session.getTransaction().commit();
        return medType;
	}
	
	public MedicineTypeEvent createAndStoreMedicineTypeEvent(String name){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();
        
        MedicineTypeEvent medTypeEvent = new MedicineTypeEvent();       
        medTypeEvent.setName(name);        
        session.save(medTypeEvent);
        session.getTransaction().commit();
        return medTypeEvent;
	}
	
	public StockTypeEvent createAndStoreStockTypeEvent(String name){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();
        
        StockTypeEvent stockTypeEvent = new StockTypeEvent();       
        stockTypeEvent.setName(name);        
        session.save(stockTypeEvent);
        session.getTransaction().commit();
        return stockTypeEvent;
	}
	

    public Medicine createAndStoreMedicine(String tagId, Date tagDate, Date expDate, Localization localization, MedicineType mtype) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Medicine theMedicine = new Medicine();
        theMedicine.setTagId(tagId);
        theMedicine.setExpDate(expDate);
        theMedicine.setTagDate(tagDate);
        theMedicine.setLocalizationId(localization);
        theMedicine.setTypeId(mtype);

        session.save(theMedicine);

        session.getTransaction().commit();
        return theMedicine;
        
    }
    
    public StockEvent createAndStoreStockEvent(MedicineType type, Date eventDate, StockTypeEvent eventType, String message,int stock) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        StockEvent stockEvent = new StockEvent();
        stockEvent.setMedicineType(type);
        stockEvent.setDate(eventDate);
        stockEvent.setTypeEventId(eventType);
        stockEvent.setMessage(message);
        stockEvent.setStock(stock);

        session.save(stockEvent);

        session.getTransaction().commit();
        return stockEvent;
        
    }

    
    public void createandStoreMedEvent(Medicine medicine, MedicineTypeEvent eventType,String message){
        
    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        MedicineEvent medEvent = new MedicineEvent();
        medEvent.setDate(new Date());
        medEvent.setTypeEventId(eventType);
        medEvent.setMedicine(medicine);
        medEvent.setMessage(message);
        
        session.save(medEvent);

        session.getTransaction().commit();
    	
    }
    
    @SuppressWarnings("unchecked")
	public List<Medicine> listMedicines() {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        List result = session.createQuery("from Medicine").list();

        session.getTransaction().commit();

        return result;
    }

    public void addMedicineType(String name, String manufacturer){
    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();
        
        MedicineType mtype= new MedicineType();
        mtype.setName(name);
        mtype.setManufacturer(manufacturer);
        
        session.save(mtype);
        session.getTransaction().commit();
        
    }
    
    public List listMedicineType(String type) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Query q=session.createQuery("select mtype from MedicineType as mtype where mtype.name = :nameMed");
        q.setParameter("nameMed", type);
        
        List result = q.list();
        	
        session.getTransaction().commit();

        return result;
    }
    
    public List listLocalization(String localization) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Query q=session.createQuery("select localization from Localization as localization where localization.name = :nameLoc");
        q.setParameter("nameLoc", localization);
        
        List result = q.list();
        	
        session.getTransaction().commit();

        return result;
    }
    
    public List listMedicineEventType(Long id) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Query q=session.createQuery("select metype from MedicineTypeEvent as metype where metype.id = :idEvent");
        q.setParameter("idEvent", id);
        
        List result = q.list();
        	
        session.getTransaction().commit();

        return result;
    }
    
    public List listStockEventType(Long id) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Query q=session.createQuery("select stype from StockTypeEvent as stype where stype.id = :idEvent");
        q.setParameter("idEvent", id);
        
        List result = q.list();
        	
        session.getTransaction().commit();

        return result;
    }

    @SuppressWarnings("deprecation")
	public List listStockEventToday(MedicineType id) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        
        Calendar c = new GregorianCalendar(); 
 	   
 	   int dia, mes, annio;
 	   
 	   dia = c.get(Calendar.DATE);
 	   mes = c.get(Calendar.MONTH);
 	   annio = c.get(Calendar.YEAR);
 	  
 	   Date fromDate = new Date(annio, mes, dia,0,0,0);
 	  Date toDate = new Date(annio, mes, dia,23,59,59);

        session.beginTransaction();

        Query q=session.createQuery("select stEvent from StockEvent as stEvent where stEvent.medicineType = :idTypeMed and stEvent.date > :fromDate and date < :toDate");
        q.setParameter("idTypeMed", id);
        q.setParameter("fromDate",fromDate);
        System.out.println("CARDEA " + fromDate);
        q.setParameter("toDate",toDate);
        System.out.println("CARDEA " + toDate);
        
        List result = q.list();
        	
        session.getTransaction().commit();

        return result;
    }

}
